package com.group.transaction.service.role.impl;

import com.alibaba.fastjson.JSONObject;
import com.group.transaction.domain.Role;
import com.group.transaction.domain.RolePermission;
import com.group.transaction.mapper.RoleMapper;
import com.group.transaction.mapper.RolePermissionMapper;
import com.group.transaction.service.role.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    /**
     * 添加角色
     * @param role
     * @return
     */
    @Override
    @Transactional
    public JSONObject addRole(Role role) {
        Integer sameNum = roleMapper.querySameRoleName(role.getName());
        if(sameNum != null && sameNum > 0) {
            return new JSONObject().fluentPut("result","该角色已经存在!!!");
        }
        int num = roleMapper.insert(role);
        if(num == 1) {
            Role data = roleMapper.selectOne(role);
            if(role.getPermission() != null && !"".equals(role.getPermission())) {
                String[] permissions = role.getPermission().split(",");
                List<String> list = new ArrayList<>();
                for(String s: permissions) {
                    boolean restart = true;
                    if(list != null && list.size() != 0) {
                        for(String str: list) {
                            if(s.equals(str)) {
                                restart = false;
                                break;
                            }
                        }
                    }
                    if(restart) {
                        list.add(s);
                        if(!"".equals(s)) {
                            RolePermission permission = new RolePermission();
                            permission.setRoleId(data.getId());
                            permission.setPermissionId(Long.parseLong(s));
                            rolePermissionMapper.insert(permission);
                        }
                    }
                }
            }
        }
        return new JSONObject().fluentPut("result",1);
    }
    /**
     * 编辑角色
     * @param role
     * @return
     */
    @Override
    @Transactional
    public JSONObject compileRole(Role role){
        Integer sameNum = roleMapper.querySameRoleName(role.getName());
        if(sameNum != null && sameNum > 0) {
            return new JSONObject().fluentPut("result","该角色已经存在!!!");
        }
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRolePermissiom(role.getId());
        if(role.getPermission() != null && !"".equals(role.getPermission())) {
            String[] permissions = role.getPermission().split(",");
            List<String> list = new ArrayList<>();
            for(String s: permissions) {
                boolean restart = true;
                if(list != null && list.size() != 0) {
                    for(String str: list) {
                        if(s.equals(str)) {
                            restart = false;
                            break;
                        }
                    }
                }
                if(restart) {
                    list.add(s);
                    if(!"".equals(s)) {
                        RolePermission permission = new RolePermission();
                        permission.setRoleId(role.getId());
                        permission.setPermissionId(Long.parseLong(s));
                        rolePermissionMapper.insert(permission);
                    }
                }
            }
        }
        return new JSONObject().fluentPut("result", 1);
    }
    /**
     * 删除角色
     * @param role
     * @return
     */
    @Override
    @Transactional
    public JSONObject deleteRole(Role role){
        return new JSONObject().fluentPut("result",roleMapper.delete(role));
    }
    /**
     * 查询角色
     * @param role
     * @return
     */
    @Override
    public  JSONObject queryRoleData(Role role){
        try {
            List<Role> roleList = roleMapper.queryRoleData(role);
            return new JSONObject().fluentPut("result",roleList).fluentPut("total",roleMapper.queryRoleDataTotal(role));
        } catch (Exception e) {
            return new JSONObject().fluentPut("result", null);
        }
    }

}
